package com.mulidatasource.demo.controller;

import com.mulidatasource.demo.entity.mysql.User;
import com.mulidatasource.demo.entity.postgres.Employee;
import com.mulidatasource.demo.repository.mysql.UserRepository;
import com.mulidatasource.demo.repository.postgres.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DemoController {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private EmployeeRepository employeeRepository;

    @GetMapping("user")
    public String user() {
        List<User> userList = userRepository.findAll();
        return userList.toString();
    }

    @GetMapping("employee")
    public String employee() {
        List<Employee> employeeList = employeeRepository.findAll();
        return employeeList.toString();
    }

    @GetMapping("saveUser")
    public String saveUser() {
        User user = new User();
        user.setAge(20);
        user.setName("张三");
        user.setNickName("最潮流的名字");
        user = userRepository.save(user);
        return user.toString();
    }

    @GetMapping("saveEmployee")
    public String saveEmployee() {
        Employee employee = new Employee();
        employee.setAge(30);
        employee.setName("王五");
        employee.setNickName("五五五");
        employee = employeeRepository.save(employee);
        return employee.toString();
    }
}
